package usr;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import database.Database;
import database.DatabaseState;



public class Usr implements DatabaseState {
	
	
	public static void main(String[] args) {
		Usr usr = new Usr();
		usr.setEmail("Bolsheviki@yeah.net");
		usr.setLogin("Bolsheviki");
		usr.setPassword("Soviet");
		
		int state =	usr.create();
		System.out.println(state);

		System.out.println();
		usr.setLogin("Bolsheviki");
		state = usr.login();
		System.out.println(state); 
		usr.display();

		System.out.println();
		usr.setLogin("Bolsheviki@yeah.net");
		state = usr.login();
		System.out.println(state);
		usr.display();
		
		System.out.println();
		usr.setLogin("sdfs");
		usr.setPassword("signal");
		state = usr.login();
		System.out.println(state);
		usr.display();
	}
	
	
	private int id;
	private String login;
	private String email;
	private String password;
	private int money = 5000;
	private int credit = 1;
	private StringBuffer log = new StringBuffer();
	
	
	public boolean createTask()			//change the money and credit
	{
		int pay = 0;
		if(credit == 0)						//top
			pay = 0;
		else if(credit >= 10000)			//level 1
			pay = 20;
		else if(credit >= 1000)				//level 2
			pay = 30;
		else if(credit >= 100)				//level 3
			pay = 40;
		else if(credit >= 10)				//level 4
			pay = 50;
		else								//level 5
			pay = 60;
		
		if(money >= pay)
		{
			money -= pay;
			credit = (credit == 0) ? 0 : credit + 1;
			return true;
		}
		else
			return false;
	}
	
	
	public int getPay()					//return how money should pay but not pay
	{
		int pay = 0;
		if(credit == 0)						//top
			pay = 0;
		else if(credit >= 10000)			//level 1
			pay = 20;
		else if(credit >= 1000)				//level 2
			pay = 30;
		else if(credit >= 100)				//level 3
			pay = 40;
		else if(credit >= 10)				//level 4
			pay = 50;
		else								//level 5
			pay = 60;
		
		return pay;
	}
	
	public Usr() {
	}
	
	public Usr(int id) {
		this.id = id;
	}

	public int create() {

		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			ResultSet rs;
			
			rs = stmt.executeQuery("select * from usr where login = '" + login + "'");
			if ( rs.next() ) return LOGIN_EXIST;

			rs = stmt.executeQuery("select * from usr where email = '" + email + "'");
			if ( rs.next() ) return EMAIL_EXIST;

			stmt.executeUpdate(String.format(
					"insert into usr(`login`, `email`, `password`, `log`) " +
					"values('%s', '%s', '%s', '%s')", 
					login, email, password, log));

			rs = stmt.executeQuery("select last_insert_id() id");
			rs.next();
			id = rs.getInt("id");
			
			stmt.close();
			connection.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
		
		return SUCCESSFUL;
	}
	
	public int modify() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
					"select * " +
					"from usr " +
					"where email = '%s' and id != %d",
					email, id));
			if ( rs.next() ) {
				return EMAIL_EXIST;
			}

			stmt.executeUpdate(String.format(
					"update usr " +
					"set email = '%s', password = '%s', " +
					"		credit = %d, money = %d " +
					"where id = %d",
					email, password, credit, money, id));

			stmt.close();
			connection.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
		
		return SUCCESSFUL;
	}
	
	public int login() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			
			ResultSet rs;
			
			rs = stmt.executeQuery(String.format(
					"select * " +
					"from usr " +
					"where login = '%s' and password = '%s'",
					login, password));
			
			if ( rs.next() ) {
				this.id = rs.getInt("id");
				this.login = rs.getString("login");
				this.email = rs.getString("email");
				this.credit = rs.getInt("credit");
				this.money = rs.getInt("money");
				this.log = new StringBuffer(rs.getString("log"));
				return SUCCESSFUL;
			}
			
			rs = stmt.executeQuery(String.format("select * from usr where email = '%s' and password = '%s'",
									login, password));
			if ( rs.next() ) {
				this.id = rs.getInt("id");
				this.login = rs.getString("login");
				this.email = rs.getString("email");
				this.credit = rs.getInt("credit");
				this.money = rs.getInt("money");
				this.log = new StringBuffer(rs.getString("log"));
				return SUCCESSFUL;
			}
			
			stmt.close();
			connection.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
		
		return LOGIN_ERROR;
	}
	
	public int getUsr() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
						"select * " +
						"from usr " +
						"where id = %d",
						id));
			
			if ( rs.next() ) {
				email = rs.getString("email");
				login = rs.getString("login");
				money = rs.getInt("money");
				password = rs.getString("password");
				credit = rs.getInt("credit");
				log = new StringBuffer(rs.getString("log"));
				
				stmt.close();
				connection.close();
			} else {
				stmt.close();
				connection.close();
				return LOGIN_ERROR;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
		
		return LOGIN_ERROR;
	}
	
	public int getUsrByLogin() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
						"select * " +
						"from `usr` " +
						"where `login` = '%s'",
						login));
			
			if ( rs.next() ) {
				id = rs.getInt("id");
				email = rs.getString("email");
				money = rs.getInt("money");
				credit = rs.getInt("credit");
				log = new StringBuffer(rs.getString("log"));

				stmt.close();
				connection.close();
				return SUCCESSFUL;
			} else {
				stmt.close();
				connection.close();
				return LOGIN_NOT_EXIST;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
	}
	
	public int getUsrByEmail() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
						"select * " +
						"from `usr` " +
						"where `email` = '%s'",
						email));
			
			if ( rs.next() ) {
				id = rs.getInt("id");
				login = rs.getString("login");
				money = rs.getInt("money");
				password = rs.getString("password");
				credit = rs.getInt("credit");
				log = new StringBuffer(rs.getString("log"));

				stmt.close();
				connection.close();
				return SUCCESSFUL;
			} else {
				stmt.close();
				connection.close();
				return LOGIN_NOT_EXIST;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
	}	
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setCredit(int credit) {
		this.credit = credit;
	}
	
	public void setMoney(int money) {
		this.money = money;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public int getId() {
		return id;
	}
	
	public String getLogin() {
		return login;
	}
	
	public String getEmail() {
		return email;
	}
	
	public String getPassword() {
		return password;
	}
	
	public String getLog() {
		return log.toString();
	}
	
	public void appLog(String s) {
		log.append(s);
	}
	
	public void clearLog() {
		log.delete(0, log.length());
	}
	
	public void writeLog() {

		try {
			Connection connection = Database.getConnection();
			Statement stmt = connection.createStatement();
			
			stmt.executeUpdate(String.format(
					"update usr " +
					"set log = '%s' " +
					"where id = %d",
					log, id));

			stmt.close();
			connection.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void display() {
		System.out.println("id : " + id);
		System.out.println("email : " + email);
		System.out.println("login : " + login);
		System.out.println("password : " + password);
		System.out.println("credit : " + credit);
		System.out.println("money : " + money);
		System.out.println("log : " + log);
	}
	
	public int getMoney() {
		return money;
	}
	
	public int getCredit() {
		return credit;
	}
	
}
